package cn.edu.hbpu.service.impl;

import cn.edu.hbpu.common.R;
import cn.edu.hbpu.entity.User;
import cn.edu.hbpu.handler.CustomException;
import cn.edu.hbpu.mapper.CommonUserMapper;
import cn.edu.hbpu.service.CommonUserService;
import cn.edu.hbpu.utils.DateTimeFormat;
import cn.edu.hbpu.utils.GenerateRandomPwd;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author lcy
 * @date 2022/8/12 16:14
 * @description
 */
@Service
public class CommonUserServiceImpl implements CommonUserService {
    @Autowired
    private CommonUserMapper commonUserMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public R<String> resetPwd(String userId) {
        // 1. 获取用户id
        if (Objects.isNull(userId)) {
            throw new CustomException("请传入用户id");
        }
        // 2. 根据用户名重置用户密码
        User user = new User();
        user.setUserId(userId);
        // 3. 随机生成一个复杂的10位密码并返回给前端
        String randomPwd = GenerateRandomPwd.generate();
        String encodePwd = passwordEncoder.encode(randomPwd);
        user.setPassword(encodePwd);
        commonUserMapper.updateById(user);
        // 3. 返回结果
        return new R<>(HttpStatus.HTTP_OK, "重置密码成功", randomPwd);
    }

    @Override
    public R<String> changeStatus(String userId, int status) {
        if (status != 1 && status != 0) {
            throw new CustomException("请传入正常的状态值");
        }
        if (Objects.isNull(userId)) {
            throw new CustomException("请传入用户id");
        }
        // 1. 查询出该id所对应的数据
        User user = commonUserMapper.selectById(userId);
        if (Objects.isNull(user)) {
            throw new CustomException("没有该用户");
        }
        // 2. 设置要修改的状态
        user.setStatus(status);
        // 3. 调用方法修改账号状态
        commonUserMapper.updateById(user);
        // 4. 返回结果
        return new R<>(HttpStatus.HTTP_OK, "状态修改成功");
    }

    @Override
    public R<String> deleteUser(String userId) {
        if (Objects.isNull(userId)) {
            throw new CustomException("请传入用户id");
        }
        // 1. 删除用户
        commonUserMapper.deleteById(userId);
        // 2. 返回结果
        return new R<>(HttpStatus.HTTP_OK, "删除用户成功");
    }

    @Override
    public R<Page<User>> pagingQuery(String searchField, String searchText, String startDateTime, String endDateTime, int page, int pageSize) {
        // 1. 非空验证
        if (Objects.isNull(searchField) || Objects.isNull(searchText)) {
            throw new CustomException("请传入搜索字段或文本");
        }
        LocalDateTime start = null;
        LocalDateTime end = null;
        // 2. 将日期进行转换
        if (!Objects.isNull(startDateTime) && !Objects.isNull(endDateTime)) {
            start = DateTimeFormat.parseTimeToUtc(startDateTime);
            end = DateTimeFormat.parseTimeToUtc(endDateTime);
        }
        // 3. 将searchField转为下划线
        searchField = StringUtils.camelToUnderline(searchField);

        // 4. 创建Page对象
        Page<User> pageInfo = new Page<>(page, pageSize);

        // 5. 创建查询构造器
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        // 6. 判断searchField的字段类型
        if (searchField.isEmpty()) {
            // 6.1 搜索字段为空时
            // 6.1.1 分页查询所有数据
            commonUserMapper.selectPage(pageInfo, null);
        } else if ("create_time".equals(searchField) || "update_time".equals(searchField)) {
            // 6.2 当搜索字段为创建时间和更新时间时
            queryWrapper.ge(searchField, start).le(searchField, end);
            commonUserMapper.selectPage(pageInfo, queryWrapper);
        } else {
            // 6.3 当搜索字段为其它字段时
            if ("status".equals(searchField)) {
                searchText = "正常".equals(searchText) ? "1" : "0";
            }
            if ("gender".equals(searchField)) {
                searchText = "男".equals(searchText) ? "1" : "0";
            }
            queryWrapper.like(searchField, searchText);
            commonUserMapper.selectPage(pageInfo, queryWrapper);
        }

        // 7. 返回结果
        return new R<>(HttpStatus.HTTP_OK, pageInfo);
    }

}
